package net.ibizsys.paas.appmodel;


import net.ibizsys.paas.util.StringHelper;
import net.sf.json.JSONObject;

/**
 * 应用框架辅助对象
 * @author Administrator
 *
 */
public abstract class AppPFHelperBase implements IAppPFHelper
{
	private IApplicationModel iApplicationModel = null;
	private String strImagesPath = "";
	
	/* (non-Javadoc)
	 * @see net.ibizsys.paas.appmodel.IAppPFHelper#init(net.ibizsys.paas.appmodel.IApplicationModel)
	 */
	@Override
	public void init(IApplicationModel iApplicationModel)  throws Exception
	{
		this.iApplicationModel = iApplicationModel;
	}
	
	/**
	 * 获取应用程序模型
	 * @return
	 */
	public IApplicationModel getAppModel()
	{
		return this.iApplicationModel;
	}
	
	
	/**
	 * 设置应用图标放置路径
	 * @param strImagesPath
	 */
	public void setImagesPath(String strImagesPath){
		this.strImagesPath = strImagesPath;
	}
	
	
	/**
	 * 获取应用图标放置路径
	 * @return
	 */
	public String getImagesPath(){
		return this.strImagesPath;
	}
	
	
	
	
	
	
	/**
	 * 获取应用视图的JSON对象
	 * @param iAppViewModel 应用视图模型对象
	 * @return
	 */
	public JSONObject getAppViewJSONObject(IAppViewModel  iAppViewModel) throws Exception
	{
		JSONObject jsonObj = new JSONObject();
		onFillAppViewJSONObject(iAppViewModel,jsonObj);
		return jsonObj;
	}
	
	
	/**
	 * 填充应用视图JSONObject对象
	 * @param iAppViewModel  应用视图模型对象
	 * @param jsonObj 填充的JSON对象
	 * @throws Exception
	 */
	protected void onFillAppViewJSONObject(IAppViewModel  iAppViewModel,JSONObject jsonObj) throws Exception
	{
		
	}

	/* (non-Javadoc)
	 * @see net.ibizsys.paas.appmodel.IAppPFHelper#mapRealUrl(java.lang.String)
	 */
	@Override
	public String mapRealUrl(String strUrl) throws Exception {
		if(StringHelper.isNullOrEmpty(strUrl))
			return strUrl;
		
		//当前绝对地址
		if((strUrl.indexOf('/')==0)||(strUrl.indexOf("../")==0)||(strUrl.indexOf("://")!=-1)){
			return strUrl;
		}
		
		return mapRealAppUrl(strUrl);
		
	}
	
	
	/**
	 * 映射实际的应用路径
	 * @param strUrl
	 * @return
	 * @throws Exception
	 */
	protected abstract String mapRealAppUrl(String strUrl) throws Exception ;

	
	/* (non-Javadoc)
	 * @see net.ibizsys.paas.appmodel.IAppPFHelper#mapImageRealUrl(java.lang.String)
	 */
	@Override
	public String mapImageRealUrl(String strImageUrl) throws Exception {
		if(StringHelper.isNullOrEmpty(strImageUrl))
			return strImageUrl;
		
		//当前绝对地址
		if((strImageUrl.indexOf('/')==0)||(strImageUrl.indexOf("../")==0)||(strImageUrl.indexOf("://")!=-1)){
			return strImageUrl;
		}
		
		
		return mapRealAppUrl(this.getImagesPath() + strImageUrl);
		
		
		
	}
	
	
}
